package yannan.voiplock4ble;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;
import yannan.voiplock4ble.base.Content;
import yannan.voiplock4ble.base.LoggedBaseActivity;
import yannan.voiplock4ble.base.MyApplication;
import yannan.voiplock4ble.bean.ble.AddPwd;
import yannan.voiplock4ble.bean.ble.AddVein;
import yannan.voiplock4ble.eventbus.AddPwdMsg;
import yannan.voiplock4ble.eventbus.AddVeinMsg;
import yannan.voiplock4ble.eventbus.BaseEventMsg;
import yannan.voiplock4ble.service.ble.BleOperUtils;
import yannan.voiplock4ble.service.ble.BleService;
import yannan.voiplock4ble.service.ble.BleUtils;
import yannan.voiplock4ble.utils.PopShowUtils;

/**
 * 说明：注册指静脉或者密码页面
 * 作者：mainTel
 * 时间：2016/12/5 15:47
 * 备注：
 */
@Content(R.layout.activity_add_key_prompt)
public class AddPwdOrVeinActivity extends LoggedBaseActivity implements View.OnClickListener {
    @Bind(R.id.ll_add_key_vein)
    LinearLayout ll_add_key_vein;

    @Bind(R.id.ll_add_key_pwd)
    LinearLayout ll_add_key_pwd;
    @Bind(R.id.btn_add_key)
    Button button;
    byte[] order;
    boolean isPwd;
    AlertDialog hintDialog;
    boolean isFromSearch;

    @Override
    public void initData() {
//        MyApplication.getInterface().activityMap.put(this.getLocalClassName(), this);
        String type = getIntent().getStringExtra("type");
        if (type != null) {
            if (type.equals("pwd")) {
                setTitleTheme(TitleTheme.TOOLBAR_THEME_COMMON, "注册密码");
                ll_add_key_vein.setVisibility(View.GONE);
                ll_add_key_pwd.setVisibility(View.VISIBLE);
                isPwd = true;
            } else {
                isFromSearch = getIntent().getBooleanExtra("isFromSearch", false);
                setTitleTheme(TitleTheme.TOOLBAR_THEME_COMMON, "注册指静脉");
                ll_add_key_vein.setVisibility(View.VISIBLE);
                ll_add_key_pwd.setVisibility(View.GONE);
                isPwd = false;
            }
        }
    }

    @OnClick(R.id.btn_add_key)
    @Override
    public void onClick(View v) {
        if (MyApplication.getInterface().mConnectLock != null && MyApplication.getInterface().mConnectLock.contentToken != 0) {
            if (isPwd) {
                order = BleUtils.registerPwd(MyApplication.getInterface().mConnectLock.contentToken);
                sendOrder();
                hintDialog = PopShowUtils.createHintDialog(mContext, false, "请验证管理员权限");
            } else {
                showAddPop();
            }
            button.setVisibility(View.GONE);
        } else {
            Toast.makeText(mContext, "蓝牙已断开，请重新连接", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private void showAddPop() {

        if (isFromSearch) {
            order = BleUtils.registerFinger(
                    MyApplication.getInterface().mConnectLock.contentToken,
                    MyApplication.getInterface().userBean.getUserAccount().getBytes(), 0);
            sendOrder();
            hintDialog = PopShowUtils.createHintDialog(mContext, false, "请验证管理员权限");
        } else {
            PopShowUtils.addVeinKey(mContext, new PopShowUtils.IAddVeinDiaClickListener() {
                @Override
                public void onClick(String msg, int power) {
                    if (msg == null) {
                        finish();
                    } else {
                        order = BleUtils.registerFinger(
                                MyApplication.getInterface().mConnectLock.contentToken,
                                msg.getBytes(), power);
                        sendOrder();
                        hintDialog = PopShowUtils.createHintDialog(mContext, false, "请验证管理员权限");
                    }
                }
            });
        }

    }

    private void sendOrder() {
        if (BleOperUtils.checkBleConnect()) {
            BleService.getBleService().sendOrder(order);
        } else {
            // TODO: 2017/2/28 提示蓝牙已断开
            Toast.makeText(mContext, "蓝牙已断开，请重新连接", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("AddPwdOrVeinActivity");
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("AddPwdOrVeinActivity");
        MobclickAgent.onResume(this);
    }

    /*
    public final static int PWD_REGISTER_SUCCESS = 0;
    public final static int PWD_VERIFY_AUTHORITY_OK = 1;
    public final static int PWD_VERIFY_AUTHORITY_FAILURE = 2;
    public final static int PWD_NUM_REACH_MAX = 3;
    public final static int PWD_SAME_HAVE_EXSITED = 4;
    public final static int PWD_SET_ENTER_TIME_OUT = 5;
    public final static int PWD_ENTER_FIRST_SUCCESS = 6;
    public final static int PWD_ENTER_FIRST_ERROR = 7;
    public final static int PWD_ENTER_SECOND_ERROR = 8;
    public final static int PWD_ENTER_SECOND_NOT_SAME = 9;
     */

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(BaseEventMsg msg) {
        //添加密码
        if (msg instanceof AddPwdMsg) {
            AddPwdMsg bean = (AddPwdMsg) msg;
            if (bean.addPwd != null) {
                if (hintDialog != null) {
                    hintDialog.dismiss();
                }
                switch (bean.addPwd.operResult) {
                    case AddPwd.PWD_REGISTER_SUCCESS: //注册成功
                        showMsg("恭喜您注册成功！", true);
                        break;
                    case AddPwd.PWD_VERIFY_AUTHORITY_OK: //验证管理员密码成功
                        hintDialog = PopShowUtils.createHintDialog(mContext, true, "验证管理员成功，请在门锁上输入密码");
                        break;
                    case AddPwd.PWD_VERIFY_AUTHORITY_FAILURE: //验证管理员密码失败
                        showMsg("管理员验证失败！", true);
                        break;
                    case AddPwd.PWD_NUM_REACH_MAX: //密码达到最大值
                        showMsg("密码用户达到最大值！无法继续注册。", true);
                        break;
                    case AddPwd.PWD_SAME_HAVE_EXSITED: //验证管理员密码失败
                        PopShowUtils.showCommonDialog(mContext, "密码已存在！是否重新注册？", "取消", "确定",
                                false, new PopShowUtils.IDialogCommonListener() {
                                    @Override
                                    public void onClick(int direction) {
                                        if (direction == 0) {
                                            BleService.getBleService().sendOrder(order);
                                        } else {
                                            finish();
                                        }
                                    }
                                });
                        break;
                    case AddPwd.PWD_ENTER_FIRST_SUCCESS: //第一次输入密码成功
                        hintDialog = PopShowUtils.createHintDialog(mContext, true, "请再次输入密码");
                        break;
                    case AddPwd.PWD_ENTER_FIRST_ERROR: //第一次输入密码失败
                        hintDialog = PopShowUtils.createHintDialog(mContext, true, "输入密码失败");
                        break;
                    case AddPwd.PWD_ENTER_FIRST_TIMEOUT: //输入超时
                        showMsg("第一次输入超时！", true);
                        break;
                    case AddPwd.PWD_ENTER_SECOND_ERROR: //
                        hintDialog = PopShowUtils.createHintDialog(mContext, true, "输入密码失败");
                        break;
                    case AddPwd.PWD_ENTER_SECOND_NOT_SAME: //
                        hintDialog = PopShowUtils.createHintDialog(mContext, true, "两次密码输入不一致，请重新输入");
                        break;
                    case AddPwd.PWD_ENTER_SECOND_TIMEOUT:
                        showMsg("第二次输入超时", true);
                        break;
                }
            }
            //添加指静脉

        } else if (msg instanceof AddVeinMsg) {
            AddVeinMsg addVeinMsg = (AddVeinMsg) msg;
            if (addVeinMsg.addVein != null) {
                if (hintDialog != null) {
                    hintDialog.dismiss();
                }
                switch (addVeinMsg.addVein.registerResult) {
                    case AddVein.FINGER_REGISTER_NOT_EXCEPT://无异常
                        if (isFromSearch) {
                            Intent intent = new Intent();
                            intent.putExtra("addVeinKeySuc", true);
                            setResult(RESULT_OK, intent);
                            finish();
                        } else {
                            PopShowUtils.showCommonDialog(mContext, "恭喜您！注册成功！是否继续注册？", "取消", "确定",
                                    false, new PopShowUtils.IDialogCommonListener() {
                                        @Override
                                        public void onClick(int direction) {
                                            if (direction == 0) {
                                                if (isPwd) {
                                                    hintDialog = PopShowUtils.createHintDialog(mContext, true, "请验证管理员权限");
                                                } else {
                                                    showAddPop();
                                                }
                                            } else {
                                                finish();
                                            }
                                        }
                                    });
                        }
                        break;
                    case AddVein.FINGER_NUM_REACH_MAX://指静脉用户个数已达到最大
                        PopShowUtils.showCommonDialog(mContext, "指静脉钥匙个数已经达到最大！无法继续注册", null, "确定",
                                false, new PopShowUtils.IDialogCommonListener() {
                                    @Override
                                    public void onClick(int direction) {
                                        if (isFromSearch) {
                                            Intent intent = new Intent();
                                            intent.putExtra("addVeinKeySuc", true);
                                            setResult(RESULT_OK, intent);
                                            finish();
                                        } else {
                                            finish();
                                        }
                                    }
                                });
                        break;
                    case AddVein.FINGER_VERIFY_OK://权限认证成功
                        hintDialog = PopShowUtils.createHintDialog1(mContext, true, "权限认证成功，请在门锁上三次伸入手指注册指静脉");
                        break;
                    case AddVein.VERIFY_ADMIN_AUTHORITY_FAILURE://权限认证失败
                        PopShowUtils.showCommonDialog(mContext, "对不起权限认证失败！是否重新注册？", "取消", "确定",
                                false, new PopShowUtils.IDialogCommonListener() {
                                    @Override
                                    public void onClick(int direction) {
                                        if (direction == 0) {
                                            BleService.getBleService().sendOrder(order);
                                        } else {
                                            finish();
                                        }
                                    }
                                });
                        break;
                    case AddVein.FINGER_PHONE_HAVE_EXISTED://手机号已经存在
                        PopShowUtils.showCommonDialog(mContext, "对不起,该手机号已存在！是否重新注册？", "取消", "确定",
                                false, new PopShowUtils.IDialogCommonListener() {
                                    @Override
                                    public void onClick(int direction) {
                                        if (direction == 0) {
                                            showAddPop();
                                        } else {
                                            finish();
                                        }
                                    }
                                });
                        break;
                    case AddVein.FINGER_REGISTER_FAILURE://指静脉注册失败
                        PopShowUtils.showCommonDialog(mContext, "指静脉注册失败！是否重新注册？", "取消", "确定",
                                false, new PopShowUtils.IDialogCommonListener() {
                                    @Override
                                    public void onClick(int direction) {
                                        if (direction == 0) {
                                            BleService.getBleService().sendOrder(order);
                                        } else {
                                            finish();
                                        }
                                    }
                                });
                        break;
                }
            }
        }
    }

    private void showMsg(String msg, final boolean isFinish) {
        PopShowUtils.showCommonDialog(mContext, msg,
                null, "确定", true,
                new PopShowUtils.IDialogCommonListener() {
                    @Override
                    public void onClick(int direction) {
                        if (isFinish) {
                            finish();
                        }
                    }
                });
    }
}
